<template>
	<div class="edit-popup">
		<popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
			@close="handleClose">
			<el-form ref="formRef" :model="formData" label-width="120px">
				<el-form-item label="上级目录" prop="pid" v-if="formData.pid !== 0">
					<el-tree-select class="flex-1" v-model="formData.pid" :data="treeList" clearable
						node-key="id" :props="{
                            value: 'id',
                            label: 'name'
                        }" check-strictly :default-expand-all="true" placeholder="请选择上级目录" />
				</el-form-item>
				<el-form-item :label="fileLable+'名称'" prop="name">
					<el-input v-model="formData.name" placeholder="请输入目录/文件名称" :maxlength="100" />
				</el-form-item>
				<el-form-item label="上传文件" prop="file_url" v-if="isParent=='false'">
					<material-picker v-model="formData.file_url" type="file"/>
				</el-form-item>
			</el-form>
		</popup>
	</div>
</template>

<script lang="ts" setup name="informationInfoEdit">
	import type { FormInstance } from 'element-plus'
	import Popup from '@/components/popup/index.vue'
	import { apiInformationInfoLists, apiInformationInfoAdd, apiInformationInfoEdit, apiInformationInfoDetail ,apiInformationInfoListsAll,apiInformationInfoGetSon} from '@/api/information_info'
	import { timeFormat } from '@/utils/util'
	import { useDictOptions } from '@/hooks/useDictOptions'
	import type { PropType } from 'vue'
	defineProps({
		dictData: {
			type: Object as PropType<Record<string, any[]>>,
			default: () => ({})
		}
	})
	
	// const { optionsData } = useDictOptions<{
	// 	dept : any[]
	// }>({
	// 	dept: {
	// 		api: apiInformationInfoListsAll,
	// 		params:{
	// 			id:595
	// 		}
	// 	}
	// })
	
	const emit = defineEmits(['success', 'close'])
	const formRef = shallowRef<FormInstance>()
	const popupRef = shallowRef<InstanceType<typeof Popup>>()
	const mode = ref('add')
	const treeList = ref<any[]>([])

	// 弹窗标题
	const popupTitle = computed(() => {
		return mode.value == 'edit' ? '编辑资料库' : '新增资料库'
	})
	
	const fileLable = ref('文件')
	const isParent = ref('false')

	// 表单数据
	const formData = reactive({
		id: '',
		pid: '',
		name: '',
		file_url:'',
		car_id:'',
		information_list_id:''
	})


	// 获取详情
	const setFormData = async (data : Record<any, any>) => {
		for (const key in formData) {
			if (data[key] != null && data[key] != undefined) {
				//@ts-ignore
				formData[key] = data[key]
			}
		}


	}

	const getDetail = async (row : Record<string, any>) => {
		const data = await apiInformationInfoDetail({
			id: row.id
		})
		setFormData(data)
	}
	
	const getAll = async (row:any) => {
		formData.car_id = row.car_id
		formData.information_list_id = row.information_list_id
		const data = await apiInformationInfoListsAll({
			car_id:row.car_id,
			information_list_id:row.information_list_id
		})
      const item = {
        id: 0,
        name: '顶级',
        children: []
      };
      item.children = data;
      treeList.value.push(item);
	}
	const getSon = async (id:any) => {
		if(id!=''){
			const data = await apiInformationInfoGetSon({
				id:id
			})

			if(data.son==true){
				fileLable.value = '目录'
				isParent.value = 'true'
				console.log(isParent)
			}
		}
		
	}

	// 提交按钮
	const handleSubmit = async () => {
		await formRef.value?.validate()
		const data = { ...formData, }
		mode.value == 'edit'
			? await apiInformationInfoEdit(data)
			: await apiInformationInfoAdd(data)
		popupRef.value?.close()
		emit('success')
	}

	//打开弹窗
	const open = (type = 'add') => {
		mode.value = type
		popupRef.value?.open()
		console.log(formData)
	
	}

	// 关闭回调
	const handleClose = () => {
		emit('close')
	}

	// const getLists = async () => {
	// 	const data : any = await apiInformationInfoLists()
	// 	const item = { id: 0, : '顶级', children: [] }
	// 	item.children = data.lists
	// 	treeList.value.push(item)
	// }

	// getLists()
	

	defineExpose({
		open,
		setFormData,
		getDetail,
		getAll,
		getSon
	})
</script>